#include "SMProcess.h"
#include "stdio.h"
#include "SC_Init.h"	
#include "SC_it.h"
#include "sc95f.h"

#include "tm1668.h"
#include "drv_sonsor_air.h"
#include "drv_rgb.h"
#include "SMProcess.h"
#include "stdio.h"
#include "toolkit.h"
#include "filterCounter.h"

#include "protol_iot_mj.h"


extern void fan_power_switch(unsigned char en);

typedef enum {
	eDevicePoweron,
	eDeviceAbnormal,    
	eDeviceStandby,
	eDeviceWork,
	eDeviceFactory,
	eDeviceMax,
}eDEVICE_STATUS;

typedef enum{
	eWorkAuto,
	eWorkSleep,
	eWorkNormal,
	eWorkForce,
	eWorkMax,
}eWORK_MODE;


typedef struct
{
	eDEVICE_STATUS deviceStatus;
	eDEVICE_STATUS lastDeviceStatus;
	char statuSwitchFlag;
}deviceSMPara_t;

typedef struct
{
	eWORK_MODE workStatus;
	eWORK_MODE lastWorkStatus;
	char modeSwitchFlag;
}workSMPara_t;

typedef eDEVICE_STATUS (*FunDeviceStatus)(deviceSMPara_t xdata * ,uint32_t xdata *);

typedef struct{
  eDEVICE_STATUS status;
  char *string;
	FunDeviceStatus function;
}deviceStatusInfo_t;

typedef eWORK_MODE (*FunWorkStatus)(workSMPara_t xdata * , uint32_t xdata *);

typedef struct{
    eWORK_MODE status;
    char *string;
	FunWorkStatus function;
}workStatusInfo_t;

eDEVICE_STATUS DevicePoweronProcess(deviceSMPara_t *para, uint32_t xdata *evt);
eDEVICE_STATUS DeviceAbnormalProcess(deviceSMPara_t *para, uint32_t xdata *evt);
eDEVICE_STATUS DeviceStandbyProcess(deviceSMPara_t *para, uint32_t xdata *evt);
eDEVICE_STATUS DeviceWorkProcess(deviceSMPara_t *para, uint32_t xdata *evt);
eDEVICE_STATUS DeviceFactoryProcess(deviceSMPara_t *para, uint32_t xdata *evt);
static const deviceStatusInfo_t xdata deviceStatus[eDeviceMax] =
{
	{eDevicePoweron, 	"Poweron", DevicePoweronProcess},
	{eDeviceAbnormal, "Abnormal", DeviceAbnormalProcess},
	{eDeviceStandby, 	"Standby", DeviceStandbyProcess},
	{eDeviceWork, 		"Work", DeviceWorkProcess},
	{eDeviceFactory, 	"Factory", DeviceFactoryProcess},
};

deviceSMPara_t deviceSMPara = {eDevicePoweron,	eDevicePoweron,	TRUE};

eWORK_MODE WorkAutoProcess(workSMPara_t *para, uint32_t xdata *evt);
eWORK_MODE WorkForceProcess(workSMPara_t *para, uint32_t xdata *evt);
eWORK_MODE WorkSleepProcess(workSMPara_t *para, uint32_t xdata *evt);
eWORK_MODE WorkNormalProcess(workSMPara_t *para, uint32_t xdata *evt);

static const workStatusInfo_t xdata workStatus[eWorkMax] =
{
	{eWorkAuto, 	"auto", WorkAutoProcess},
	{eWorkSleep, 	"sleep", WorkSleepProcess},
	{eWorkNormal, "normal", WorkNormalProcess},
	{eWorkForce, "power", WorkForceProcess},
};

workSMPara_t  workSMPara = {eWorkAuto, eWorkAuto, TRUE};
workSMPara_t* workSMPtr = &workSMPara;

static eDEVICE_STATUS retStatus =eDeviceWork;
static eWORK_MODE retMode = eWorkAuto;

static uint8_t xdata statuStep = 0;
static uint32_t xdata statusTick = 0;
static uint8_t xdata modeStep = 0;
static uint32_t xdata modeTick = 0;
static uint32_t xdata forceModeTick = 0;
static uint32_t xdata normalModeTick = 0;

static uint32_t xdata airLevelTick = 0;
static uint32_t xdata airValueTick = 0;

static uint32_t xdata reduceLightTick = 0;
static uint32_t xdata standbyTestTick = 0;
static uint32_t xdata iotDataUpdataTick = 0;

#define AIR_LEVEL_MAX 5
uint16_t code pm2d5LevelTable[AIR_LEVEL_MAX] = {1, 36, 76, 151, 251};
uint32_t code timCountTable[] = {0,60*60, 120*60, 240*60, 480*60};
//uint32_t code timCountTable[] = {0,60*2, 120*2, 240*2, 480*2};
uint8_t xdata fanLevel = 0;
uint8_t xdata timingCount = 0;
uint32_t xdata timSetTick = 0;

uint8_t xdata airQualityLevel = 0;
uint16_t xdata pm2d5DispValue = 0;

uint16_t xdata pm2d5DispValueLast = 0;
uint16_t xdata fanLevelshow = 0;

//extern xdata uint32_t countDownTime;
extern xdata uint32_t countDownFlag;

bit FirstBootFlag = FALSE;
bit timeSetFlag = 0;
bit sleepCheckFlag = 0;
bit statuSwitchFlag_test = TRUE;
bit modeSwitchFlag_test = TRUE;

bit disinfectPreSleepStatusFlag = FALSE;
bit disinfectSleepStatusFlag = FALSE;
bit disinfectSleepOperateFlag = FALSE;

bit ionPreSleepStatusFlag = FALSE;
bit ionSleepStatusFlag = FALSE;
bit ionSleepOperateFlag = FALSE;

//uint8_t fanOutDuty = 20;
uint8_t fanOutDuty = 78;


//extern bool filterSwitch;
//extern EVT_STATUS evt_Filter;
extern uint32_t get_Systick(void);

extern bool reduceLightEvt ;
extern bool reduceLightFlag;
extern bool reducelightStep;
extern void reduceLightCountClear();

extern bool iot_sensor_updata_flag;
extern bool iot_state_updata_flag;
extern bool iot_abnor_updata_flag;
extern bool iot_restore_flag;
extern uint8_t iot_request_flag;

extern uint16_t xdata IotPm2d5Value;  
extern uint16_t xdata IotDeviceMode;
extern uint16_t xdata IOTFanLevel;
extern uint16_t xdata IOTKillState;

extern bool iot_model_is_ok;
extern iot_device_properties_t iotDevProps;
extern iot_device_properties_t iotDevPropsSet;
//static message_queue_t msgQueueRecv = {0};
extern uint32_t iotNetState;


volatile bool  xdata SLEEP_ONOFF_EVENT = FALSE;
uint8_t getFanLevel(void)
{
	return fanLevel;
}

void setFanLevel(uint8_t level)
{
	fanLevel = level;
}

void RGBReduceLightSwitch(uint8_t en)
{
	if(en)
	{
		tm1668_key_light_set(7);
		tm1668_disp_light_set(7);
		RGB_light_set(100);
	}
	else
	{
		tm1668_key_light_set(2);	///
		tm1668_disp_light_set(2);	///
		RGB_light_set(40);
	}
}

void Key_light_recover(void)
{
	// if(timingCount == 0 )
	// {
	// 	tm1668_Key_single_set(ePanelFlagTimer, 0);
	// }
	if(disinfect_status_get() == 0)
	{
		tm1668_Key_single_set(ePanelFlagDisinfect,0);
	}
	if(ion_status_get() == 0)
	{
		tm1668_Key_single_set(ePanelFlagIon,0);
	}
}

void getAirQualityLevel(void)
{
	uint16_t temp = 0;
	uint8_t airLevel_i = 0;
	
	temp = sensor_air_info_get();
	
	if(temp == 0){
		pm2d5DispValue = temp;
		airQualityLevel = 1;
	}else
	if(temp == pm2d5LevelTable[airQualityLevel])
	{
		if(device_timeout_count(&airLevelTick, 10000) ==  TIME_OUT_WORK)
			airQualityLevel = airQualityLevel +1;
		else
			pm2d5DispValue = pm2d5LevelTable[airQualityLevel] -1;
	}else
	if(temp == (pm2d5LevelTable[airQualityLevel-1] -1))
	{
		if(device_timeout_count(&airLevelTick, 10000) ==  TIME_OUT_WORK)
			airQualityLevel = airQualityLevel -1;
		else
			pm2d5DispValue = pm2d5LevelTable[airQualityLevel-1];
	}else
	{
		if((pm2d5DispValueLast == (temp+1)) || (pm2d5DispValueLast == (temp-1))){
		
			if(device_timeout_count(&airValueTick, 3000) ==  TIME_OUT_WORK){
				pm2d5DispValueLast = sensor_air_info_get();
				pm2d5DispValue = temp;
			}
		}else{
			pm2d5DispValueLast = sensor_air_info_get();
			pm2d5DispValue = temp;
			airValueTick = 0;
		}
		
		airLevelTick = 0;
		if(temp < pm2d5LevelTable[1])
			airQualityLevel = 1;
		else
		if(temp < pm2d5LevelTable[2])
			airQualityLevel = 2;
		else
		if(temp < pm2d5LevelTable[3])
			airQualityLevel = 3;
		else
		if(temp < pm2d5LevelTable[4])
			airQualityLevel = 4;
		else
			airQualityLevel = 5;
		
	}
	
}

void airQualityRgbDisp(void)
{
	if(pm2d5DispValue == 0){
		tm1668_num_val_set(0xffff);		//显示PM2.5
	}else
	{
		tm1668_num_val_set(pm2d5DispValue);		//显示PM2.5
	}
	rgbLedSetColor(airQualityLevel);
//	switch(airQualityLevel)
//	{
//		case 1: 
//			rgbLedSetColor(RGB_GREEN);
//			break;
//		case 2: 
//			rgbLedSetColor(RGB_BLUE);
//			break;
//		case 3: 
//			rgbLedSetColor(RGB_YELLOW);
//			break;
//		case 4: 
//			rgbLedSetColor(RGB_ORANGE);
//			break;
//		case 5: 
//			rgbLedSetColor(RGB_RED);
//			break;
//		default: 
//			rgbLedSetColor(RGB_BLACK);
//			break;
//	}
	
}
void TestRgbDisp(void)
{
	static xdata uint8_t value = 0; 
	value ++; 
//	switch(value)
//	{
//		case 1: 
//			rgbLedSetColor(RGB_GREEN);
//			break;
//		case 2: 
//			rgbLedSetColor(RGB_BLUE);
//			break;
//		case 3: 
//			rgbLedSetColor(RGB_YELLOW);
//			break;
//		case 4: 
//			rgbLedSetColor(RGB_ORANGE);
//			break;
//		case 5: 
//			rgbLedSetColor(RGB_RED);
//			break;
//		default: 
//			rgbLedSetColor(RGB_BLACK);
//			break;
//	}
	rgbLedSetColor(value);
	if(value == 5)
	{
		value = 0;
	}
}


void fanLevelRgbDisp(uint8_t value)
{
	only_num_val_set(value,1);		//显示风扇挡位
//	switch(airQualityLevel)
//	{
//		case 1: 
//			rgbLedSetColor(RGB_GREEN);
//			break;
//		case 2: 
//			rgbLedSetColor(RGB_BLUE);
//			break;
//		case 3: 
//			rgbLedSetColor(RGB_YELLOW);
//			break;
//		case 4: 
//			rgbLedSetColor(RGB_ORANGE);
//			break;
//		case 5: 
//			rgbLedSetColor(RGB_RED);
//			break;
//		default: 
//			rgbLedSetColor(RGB_BLACK);
//			break;
//	}
	rgbLedSetColor(airQualityLevel);
}

void airQualityControlFan(void)
{
	fan_level_control(airQualityLevel);//airQualityLevel
	fanLevel = airQualityLevel;
}

// void timSetControl(uint8_t cmdValue)
// {
// 	if(cmdValue == 0){
// 		timingCount = 0;
// 	}else
// 	if(cmdValue == 1){
// 		timingCount ++;
// 		timingCount = timingCount%5;
// 		tm1668_flag_set(ePanelFlagTimingHour, timingCount);
// 	}
	
// 	if(timingCount != 0){
// 		countDownTime = timCountTable[timingCount];
// 		countDownFlag = 1;		
// 	}else{
// 		countDownTime = 0;
// 		countDownFlag = 0;			
// 	}	
// }

eDEVICE_STATUS DevicePoweronProcess(deviceSMPara_t *para, uint32_t xdata *evt)
{
	uint8_t temp = 0;

	if(statuSwitchFlag_test == TRUE)
	{
		statuSwitchFlag_test = FALSE;
		printf("device status work init.\r\n");
		retStatus = eDevicePoweron;
		statuStep = 0;
	  	statusTick = 0;
	}

	if(statuStep == 0){
		statuStep = 1;
		beep_on();	
		sensor_air_work_control(OFF);
		printf("power on first step.\r\n");
	}else
	if(statuStep == 1){
		if(device_timeout_count(&statusTick, 10) ==  TIME_OUT_WORK){
			statuStep = 2;
			/*All lights on*/
			rgbLedSetColor(RGB_WHITE);
			tm1668_flag_all_control(1);
			printf("power on second step.\r\n");
		}
	}else
	if(statuStep == 2){
		if(device_timeout_count(&statusTick, 1000) ==  TIME_OUT_WORK){
			statuStep = 3;
				
			printf("power on third step.\r\n");
			retStatus = eDeviceWork;
		}
	}
	return retStatus;
}
	
eDEVICE_STATUS DeviceAbnormalProcess(deviceSMPara_t *para, uint32_t xdata *evt)
{

	if(statuSwitchFlag_test == TRUE)
	{
		statuSwitchFlag_test = FALSE;
		retStatus = eDeviceStandby;
		statuStep = 0;
		statusTick = 0;
	}
	return retStatus;
}

eDEVICE_STATUS DeviceStandbyProcess(deviceSMPara_t *para, uint32_t xdata *evt)
{

	if(statuSwitchFlag_test == TRUE)
	{
		statuSwitchFlag_test = FALSE;
		printf("standby status enter.\r\n");
		retStatus = eDeviceStandby;
		
		fan_power_switch(0);
		disinfect_model_control(OFF);
		ion_model_control(OFF);
		tm1668_flag_all_control(0);
		tm1668_Key_single_set(ePanelFlagPower, OFF);
		sensor_air_work_control(OFF);
		tm1668_flag_set(ePanelFlagFilter, OFF);
		fanLevel = 0;
		statuStep = 0;
		rgbLedSetColor(RGB_BLACK);
		tm1668_disp_light_set(0);
		tm1668_key_light_set(7);
		iotDevProps.deviceSwitch = 0;
		return retStatus;	
	}

	if( FirstBootFlag == FALSE)
	{
		if((*evt) == BTN_FILTER_SPEED_UP_EVENT)
		{
			tm1668_disp_light_set(7);
			only_num_val_set(1,1);
			filter_speed_up_init();
			beep_on();
		}

//		if((*evt) == BTN_FACTORY_TEST_MODE_EVENT)
//		{
//			beep_on();
//			retStatus = eDeviceFactory;
//		}

	}

	if((*evt) == BTN_ONOFF_EVENT)
	{
		printf("standby on off handle\r\n");
		beep_on();
		retStatus = eDeviceWork;
		// filterSwitch=TRUE;
		// evt_Filter = eBoot;
	}
	
	if((*evt) == IOT_ONOFF_EVENT)
	{	
		//if(iotDevPropsSet.deviceSwitch == 1){
			retStatus = eDeviceWork;
			beep_on();
		//}
	}

	if((*evt) != 0){
		printf("evt value :%x\r\n", (*evt)<<16);
	}

	if(retStatus != eDeviceStandby)
	{
		
	}
	return retStatus;

}


static uint32_t xdata FactoryTestTick0 = 0;
static uint32_t xdata FactoryTestTick1 = 0;

eDEVICE_STATUS DeviceFactoryProcess(deviceSMPara_t *para, uint32_t xdata *evt)
{
//	static bit onFlag = TRUE;
//	static bit ionFlag = TRUE;
//	static bit amdFlag = TRUE;
//	static bit modeFlag = TRUE;
//	static bit filterFlag = TRUE;
//	static bit wifiFlag = TRUE;
//	

//	if(statuSwitchFlag_test == TRUE)
//	{
//		statuSwitchFlag_test = FALSE;
//		printf("Factory status enter.\r\n");
//		retStatus = eDeviceFactory;
//		statuStep = 0;
//		//iot_state_updata_flag = 1;

//		RGBReduceLightSwitch(1);
//	}

//	if(statuStep == 0)
//	{
//		tm1668_flag_set(ePanelFlagDisinfect, 1);
//		tm1668_flag_set(ePanelFlagIon, 1);
//		tm1668_flag_set(ePanelFlagMode, 4);

//		tm1668_Key_single_set(ePanelFlagPower, onFlag);
//		tm1668_Key_single_set(ePanelFlagMode, modeFlag);
//		tm1668_flag_set(ePanelFlagFilter,filterFlag);
//		
//		tm1668_flag_set(ePanelFlagWifi,wifiFlag);
//		
//		tm1668_Key_single_set(ePanelFlagIon,ionFlag);
//		tm1668_Key_single_set(ePanelFlagDisinfect,amdFlag);

//		if(device_timeout_count(&FactoryTestTick1, 1000) ==  TIME_OUT_WORK)
//		{
//			statuStep =1;
//		}
//	}
//	else if(statuStep == 1)
//	{
//		tm1668_num_val_set(888);
//		if(device_timeout_count(&FactoryTestTick0, 1000) ==  TIME_OUT_WORK)
//		{
//			statuStep =2;
//		}	
//	}
//	else if(statuStep == 2)
//	{
//		tm1668_num_val_set(SOFTWARE_VERSION_F20_NUM);	//显示版本号
//		if(device_timeout_count(&FactoryTestTick0, 1000) ==  TIME_OUT_WORK)
//		{
//			statuStep =1;
//		}
//	}

//	if((*evt) == BTN_ONOFF_EVENT)
//	{
//		onFlag = !onFlag;
//		tm1668_Key_single_set(ePanelFlagPower, (uint8_t)onFlag);
//		beep_on();
//	}
//	if((*evt) == BTN_MODE_EVENT)
//	{
//		modeFlag = !modeFlag;
//		//tm1668_flag_set(ePanelFlagBGL, (uint8_t)modeFlag);
//		tm1668_Key_single_set(ePanelFlagMode,(uint8_t) modeFlag);
//		beep_on();
//	}
//	if(((*evt) == BTN_ION_EVENT) || ((*evt) == IOT_ION_EVENT))
//	{
//		ionFlag = !ionFlag;
//		tm1668_Key_single_set(ePanelFlagIon,(uint8_t)ionFlag);
//		beep_on();
//	}

//	if(((*evt) == BTN_AMD_EVENT) || ((*evt) == IOT_AMD_EVENT))
//	{
//		amdFlag = !amdFlag;
//		tm1668_Key_single_set(ePanelFlagDisinfect,(uint8_t)amdFlag);
//		beep_on();
//	}

//	if((*evt) == BTN_FILTER_RESET_EVENT)
//	{
//		filterFlag = !filterFlag;
//		tm1668_flag_set(ePanelFlagFilter, (uint8_t)filterFlag);
//		beep_on();
//	}
//	
//	if((*evt) == BTN_MATCH_NETWORK_EVENT)
//	{
//		wifiFlag = !wifiFlag;
//		tm1668_flag_set(ePanelFlagWifi, (uint8_t)wifiFlag);
//		beep_on();
//	}

//	if(device_timeout_count(&FactoryTestTick1, 1000) ==  TIME_OUT_WORK)
//	{
//		TestRgbDisp();	//RGB循环点亮
//	}

//	if(retStatus != eDeviceFactory)
//	{
//		
//	}
	return retStatus;	
}

eDEVICE_STATUS DeviceWorkProcess(deviceSMPara_t *para, uint32_t xdata *evt)
{

	if(statuSwitchFlag_test == TRUE)
	{
		iotDevProps.deviceSwitch = 1;
		statuSwitchFlag_test = FALSE;
		modeSwitchFlag_test = TRUE;
		workSMPara.lastWorkStatus = eWorkAuto;
		workSMPara.workStatus = eWorkAuto;
		retStatus =eDeviceWork;
		retMode = eWorkAuto;
		statuStep = 0;
		
		tm1668Init();
		fan_power_switch(0);
		disinfect_model_control(OFF);
		ion_model_control(OFF);
		tm1668_flag_all_control(0);
		tm1668_Key_single_set(ePanelFlagPower, OFF);
		sensor_air_work_control(OFF);
		tm1668_flag_set(ePanelFlagFilter, OFF);
		fanLevel = 0;
		statuStep = 0;
		rgbLedSetColor(RGB_BLACK);
		tm1668_disp_light_set(0);
		tm1668_key_light_set(7);
		iotDevProps.deviceSwitch = 0;
		
		sensor_air_work_control(ON);
		fanLevel = 1;
		disinfect_model_control(1);
		ion_model_control(1);
		
		tm1668_Key_single_set(ePanelFlagPower, 1);
		tm1668_Key_single_set(ePanelFlagMode, 1);
		tm1668_flag_set(ePanelFlagDisinfect, disinfect_status_get());
		tm1668_flag_set(ePanelFlagIon, ion_status_get());
		Key_light_recover();
		RGBReduceLightSwitch(1);
		RGB_light_set(100);
		tm1668_flag_set(ePanelFlagWifi,iotNetState);

		filter_state_set(Boot);
		
		iotDataUpdataTick = 0;
		iot_state_updata_flag = 1;
		
		printf("work status enter.\r\n");
		
		return retStatus;	
	}

	if((*evt) == 0){
		if(device_timeout_count(&reduceLightTick, 180000) ==  TIME_OUT_WORK){
			if(workSMPara.workStatus != eWorkSleep){
				RGBReduceLightSwitch(0);
				//printf("reduce all light value.\r\n");				
			}
		}
	}else{
		reduceLightTick = 0;
		if((workSMPara.workStatus != eWorkSleep) && ((*evt) != BTN_ONOFF_EVENT))
			RGBReduceLightSwitch(1);
	}

	retMode = workStatus[workSMPara.workStatus].function(&workSMPara, evt);

	if(device_timeout_count(&iotDataUpdataTick, 3000) ==  TIME_OUT_WORK){
		IotPm2d5Value = pm2d5DispValue;
		iot_sensor_updata_flag = 1;
	}
	

	if((*evt) == BTN_ONOFF_EVENT)
	{	
		beep_on();
		retStatus = eDeviceStandby;
	}
	
	if((*evt) == IOT_ONOFF_EVENT)
	{	
		//if(iotDevPropsSet.deviceSwitch == 0){
			retStatus = eDeviceStandby;
			beep_on();
		//}
	}
	
	if((*evt) == IOT_WIFI_STA_UPDATE)
	{	
		//if(iotDevPropsSet.deviceSwitch == 0){
		if(iotNetState){
			tm1668_flag_set(ePanelFlagWifi,1);
		}else
		{
			tm1668_flag_set(ePanelFlagWifi,0);
		}
			//beep_on();
		//}
	}
	
	if((*evt) == BTN_MATCH_NETWORK_EVENT)
	{	
		beep_on();
		iot_restore_flag = 1;
	}

//	if((*evt) == SYS_FILTER_RESET_EVENT)
//	{
//		//beep_on();
//		retStatus = eDeviceStandby;
//	}
	
	if((*evt) == BTN_FILTER_RESET_EVENT)
	{
		tm1668_flag_set(ePanelFlagFilter, OFF);
		filter_state_set(Reset);
		beep_on();
	}
	
	if((*evt) == SYS_FILTER_TIMEOUT_EVENT)
	{
		tm1668_flag_set(ePanelFlagFilter, ON);
	}

	if(workSMPara.workStatus != retMode)
	{
		printf("Work status change form [%s] to [%s].\r\n", workStatus[workSMPara.workStatus].string, workStatus[retMode].string);
		workSMPara.lastWorkStatus = workSMPara.workStatus;
		workSMPara.workStatus = retMode;
		modeSwitchFlag_test = TRUE;
	}
	
	getAirQualityLevel();

	if(statuStep == 0){
		fan_power_switch(1);
		statuStep = 1;
	}else
	if(statuStep == 1){
		if(device_timeout_count(&statusTick, 100) ==  TIME_OUT_WORK){
			//fan_level_control(1);
			statuStep = 2;
			printf("WORK second step.\r\n");
		}
	}else
	if(statuStep == 2){
		if(device_timeout_count(&statusTick, 1000) ==  TIME_OUT_WORK){
			statuStep = 3;	
			printf("WORK third step.\r\n");
		}
	}else
	if(statuStep == 3){
		fan_level_control(fanLevel);
	}

	if(retStatus != eDeviceWork)
	{
		fanLevel = 0;
		fan_level_control(0);
		filter_state_set(Shutdown);
	}
	
	iotDevProps.anionSwitch = disinfect_status_get();
	iotDevProps.plasmaSwitch = ion_status_get();
	iotDevProps.pm2d5Value = pm2d5DispValue;
	iotDevProps.airQualityValue = airQualityLevel;
	iotDevProps.deviceMode = workSMPara.workStatus;
	iotDevProps.fanLevel = fanLevel;
	
	
	return retStatus;

}

eWORK_MODE WorkAutoProcess(workSMPara_t *para, uint32_t xdata *evt)
{
	retMode = eWorkAuto;
	if(modeSwitchFlag_test == TRUE)
	{
		modeSwitchFlag_test = FALSE;
		modeStep = 0;
		modeTick = 0;
		fanLevel = 1;
		// tm1668_flag_set(ePanelFlagFilter, filter_status_get());
		tm1668_flag_set(ePanelFlagMode,  3);
		Key_light_recover();
		printf("auto mode enter.\r\n");
		
		return retMode;	
	}

	if((*evt) == BTN_MODE_EVENT)
	{
		beep_on();
		Key_light_recover();
//		fanOutDuty = fanOutDuty + 1;
//		if(fanOutDuty >= 90)
//			fanOutDuty = 20;
//		tm1668_num_val_set(fanOutDuty);
//		fan_set_value(fanOutDuty);
		retMode = eWorkSleep;
	}
	
//	if((*evt) == IOT_MODE_SLEEP_EVENT)
//	{
//		beep_on();
//		Key_light_recover();
//		
//		retMode = eWorkSleep;
//	}
	
//	if((*evt) == IOT_MODE_AUTO_EVENT)
//	{
//		beep_on();
//		Key_light_recover();
//		
//		retMode = eWorkAuto;
//	}
//	
//	if((*evt) == IOT_MODE_NORMAL_EVENT)
//	{
//		beep_on();
//		Key_light_recover();
//		
//		retMode = eWorkNormal;
//	}
	if((*evt) == IOT_MODE_EVENT)
	{
		beep_on();
		Key_light_recover();
		retMode = iotDevPropsSet.deviceMode;
	}
	
	if((*evt) == IOT_FAN_EVENT)
	{
		retMode = eWorkNormal;
		fanLevel = iotDevPropsSet.fanLevel;
		beep_on();			
	}
	
	
	
	// if((*evt) == BTN_TIM_EVENT)
	// {
	// 	//timSetControl(1);
	// 	Key_light_recover();
	// 	beep_on();
	// }
	
	if(((*evt) == BTN_AMD_EVENT) || ((*evt) == IOT_AMD_EVENT))
	{
		//disinfect_status_invert();
		disinfect_status_invert();
		tm1668_flag_set(ePanelFlagDisinfect, disinfect_status_get());
		beep_on();
		printf("disinfect status:%s\r\n", disinfect_status_get()?"on":"off");
	}

	if(((*evt) == BTN_ION_EVENT) || ((*evt) == IOT_ION_EVENT))
	{
		ion_status_invert();
		tm1668_flag_set(ePanelFlagIon, ion_status_get());
		beep_on();
		printf("ion status:%s\r\n", ion_status_get()?"on":"off");
	}
	
	if((*evt) == BTN_MATCH_NETWORK_EVENT)
	{
		//beep_on();
		iot_request_flag = 0x01;
	}

	airQualityRgbDisp();
	airQualityControlFan();

	if(retMode != eWorkAuto)
	{

	}

	return retMode;

}

eWORK_MODE WorkForceProcess(workSMPara_t *para, uint32_t xdata *evt)
{

//	if(modeSwitchFlag_test == TRUE)
//	{
//		modeSwitchFlag_test = FALSE;
//		reduceLightFlag = TRUE;
//		//fan_level_control(5);
//		modeStep = 0;
//		fanLevel = 1;
//		fanLevelshow = 1;
//		forceModeTick = 0;
//		retMode = eWorkForce;
//		// tm1668_flag_set(ePanelFlagFilter, filter_status_get());
//		tm1668_flag_set(ePanelFlagMode,  2);
//		printf("force mode enter.\r\n");
//		return retMode;	
//	}
//	
//	if((*evt) == BTN_MODE_EVENT)
//	{
//		beep_on();
//		Key_light_recover();
//		modeStep = 0;
//	}
//	
////	if((*evt) == IOT_MODE_SLEEP_EVENT)
////	{
////		beep_on();
////		Key_light_recover();
////		
////		retMode = eWorkSleep;
////	}
//	
////	if((*evt) == IOT_MODE_AUTO_EVENT)
////	{
////		beep_on();
////		Key_light_recover();
////		
////		retMode = eWorkAuto;
////	}
////	
////	if((*evt) == IOT_MODE_NORMAL_EVENT)
////	{
////		beep_on();
////		Key_light_recover();
////		
////		retMode = eWorkNormal;
////	}	
//	
//	if(modeStep == 0)
//	{
//		fanLevelRgbDisp(fanLevelshow);
//		if((*evt) == BTN_MODE_EVENT)
//		{
//			if(fanLevelshow < 3)
//			{
//				fanLevel += 2;
//				fanLevelshow += 1;
//			}
//			else
//			{
//				retMode = eWorkAuto;
//			}
//			forceModeTick = 0;
//		} 

//		if(device_timeout_count(&forceModeTick, 5000) ==  TIME_OUT_WORK)
//		{
//			modeStep = 1;
//		}
//	}
//	else if(modeStep == 1)
//	{
//		airQualityRgbDisp();
//	}


//	if((*evt) == BTN_AMD_EVENT)
//	{
//		disinfect_status_invert();

//		tm1668_flag_set(ePanelFlagDisinfect, disinfect_status_get());
//		beep_on();
//		printf("disinfect status:%s\r\n", disinfect_status_get()?"on":"off");
//	}

//	if((*evt) == BTN_ION_EVENT)
//	{
//		ion_status_invert();
//		tm1668_flag_set(ePanelFlagIon, ion_status_get());
//		beep_on();
//		printf("ion status:%s\r\n", ion_status_get()?"on":"off");
//	}
//	
//	if((*evt) == BTN_MATCH_NETWORK_EVENT)
//	{
//		//beep_on();
//		iot_request_flag = 0x01;
//	}
//	
//	// if((*evt) == BTN_TIM_EVENT)
//	// {
//	// 	timSetControl(1);
//	// 	beep_on();
//	// }

//	if(retMode != eWorkForce)
//	{

//	}

	return retMode;

}

eWORK_MODE WorkSleepProcess(workSMPara_t *para, uint32_t xdata *evt)
{

	if(modeSwitchFlag_test == TRUE)
	{
		modeSwitchFlag_test = FALSE;
		printf("sleep mode enter.\r\n");
		fanLevel = 6;
		retMode = eWorkSleep;
		modeStep = 0;
		modeTick = 0;
		tm1668_flag_set(ePanelFlagMode,  1);
		disinfectSleepOperateFlag = FALSE;
		disinfectPreSleepStatusFlag = disinfect_status_get();
		disinfectSleepStatusFlag = disinfect_status_get();
		
		ionSleepOperateFlag = FALSE;
		ionPreSleepStatusFlag = ion_status_get();
		ionSleepStatusFlag = ion_status_get();

		return retMode;	
	}
	
//	if((*evt) == IOT_MODE_SLEEP_EVENT)
//	{
//		beep_on();
//		Key_light_recover();
//		
//		retMode = eWorkSleep;
//	}
//	
//	if((*evt) == IOT_MODE_AUTO_EVENT)
//	{
//		beep_on();
//		Key_light_recover();
//		
//		retMode = eWorkAuto;
//	}
//	
//	if((*evt) == IOT_MODE_NORMAL_EVENT)
//	{
//		beep_on();
//		Key_light_recover();
//		
//		retMode = eWorkNormal;
//	}	

	if(modeStep == 0){
		airQualityRgbDisp();
		
		if(((*evt) == BTN_AMD_EVENT) || ((*evt) == IOT_AMD_EVENT))
		{
			if(disinfectSleepStatusFlag == TRUE){
				disinfect_model_control(0);
				disinfectSleepStatusFlag = FALSE;

			}else{
				disinfect_model_control(1);
				disinfectSleepStatusFlag = TRUE;
			}
			tm1668_flag_set(ePanelFlagDisinfect, disinfect_status_get());
			disinfectSleepOperateFlag = TRUE;
			modeTick = 0;
			beep_on();
			printf("disinfect status:%s\r\n", disinfect_status_get()?"on":"off");		
		}

		if(((*evt) == BTN_ION_EVENT) || ((*evt) == IOT_ION_EVENT))
		{
			if(ionSleepStatusFlag == TRUE){
				ion_model_control(0);
				ionSleepStatusFlag = FALSE;

			}else{
				ion_model_control(1);
				ionSleepStatusFlag = TRUE;
			}
			tm1668_flag_set(ePanelFlagIon, ion_status_get());
			ionSleepOperateFlag = TRUE;
			modeTick = 0;			
			
//			ion_status_invert();
//			tm1668_flag_set(ePanelFlagIon, ion_status_get());
			beep_on();
			printf("ion status:%s\r\n", ion_status_get()?"on":"off");		
		}
		
		if((*evt) == BTN_MODE_EVENT)
		{
			retMode = eWorkNormal;
			fanLevel = 1;
			beep_on();
		}
		
		if((*evt) == IOT_MODE_EVENT)
		{
			beep_on();
			//Key_light_recover();
			retMode = iotDevPropsSet.deviceMode;
		}
		
		if((*evt) == IOT_FAN_EVENT)
		{
			retMode = eWorkNormal;
			fanLevel = iotDevPropsSet.fanLevel;
			beep_on();			
		}
		
		// if((*evt) == BTN_TIM_EVENT)
		// {
		// 	timSetControl(1);
		// 	beep_on();
		// }

		if((*evt) != 0)
		{
			modeTick = 0;
		}
	
		if(device_timeout_count(&modeTick, 5000) ==  TIME_OUT_WORK)
		{
			modeStep = 1;
			rgbLedSetColor(RGB_BLACK);
			tm1668_num_disp_off();
			tm1668_disp_light_set(2);		//降低指示灯亮度
			tm1668_key_light_set(0);		//关闭按键灯
			//tm1668_flag_set(ePanelFlagFilter, OFF);
			
			if(disinfectSleepOperateFlag == FALSE )
			{
				disinfectSleepStatusFlag = FALSE;
				disinfect_model_control(0);
				tm1668_flag_set(ePanelFlagDisinfect, disinfect_status_get());
			}
			
			if(ionSleepOperateFlag == FALSE )
			{
				ionSleepStatusFlag = FALSE;
				ion_model_control(0);
				tm1668_flag_set(ePanelFlagIon, ion_status_get());
			}
			printf("sleep work mode step to 1.\r\n");
		}
	}
	else if(modeStep == 1)
	{
//		if(/*(*evt) == BTN_TIM_EVENT||*/ (*evt) == BTN_MODE_EVENT ||(*evt) == BTN_AMD_EVENT	\
//								 ||(*evt) == BTN_ONOFF_EVENT ||(*evt) == BTN_ION_EVENT)
		if((*evt))
		{
			modeStep = 0;
			beep_on();
			tm1668_key_light_set(7);
			tm1668_disp_light_set(7);
			(*evt) = 0;
		}

		if((*evt) == BTN_FILTER_RESET_EVENT)
			(*evt) = 0;
				
	 }
 
	if(retMode != eWorkSleep)
	{
		if((disinfectSleepOperateFlag == FALSE) && (disinfectPreSleepStatusFlag == TRUE) )
		{
			disinfect_model_control(1);
			tm1668_flag_set(ePanelFlagDisinfect, disinfect_status_get());
		}
		
		if((ionSleepOperateFlag == FALSE) && (ionPreSleepStatusFlag == TRUE) )
		{
			ion_model_control(1);
			tm1668_flag_set(ePanelFlagIon, ion_status_get());
		}
	}
	return retMode;
}

eWORK_MODE WorkNormalProcess(workSMPara_t *para, uint32_t xdata *evt)
{

	if(modeSwitchFlag_test == TRUE)
	{
		modeSwitchFlag_test = FALSE;
		printf("auto mode enter.\r\n");
		retMode = eWorkNormal;
		tm1668_flag_set(ePanelFlagMode,  2);
		modeStep = 0;
		modeTick = 0;
		
		normalModeTick = 0;
		
		return retMode;	
	}

	
//	if((*evt) == IOT_MODE_SLEEP_EVENT)
//	{
//		beep_on();
//		Key_light_recover();
//		
//		retMode = eWorkSleep;
//	}
//	
//	if((*evt) == IOT_MODE_AUTO_EVENT)
//	{
//		beep_on();
//		Key_light_recover();
//		
//		retMode = eWorkAuto;
//	}
//	
//	if((*evt) == IOT_MODE_NORMAL_EVENT)
//	{
//		beep_on();
//		Key_light_recover();
//		fanLevel =  IOTFanLevel;
//		retMode = eWorkNormal;
//	}
	if((*evt) == IOT_MODE_EVENT)
	{
		beep_on();
		Key_light_recover();
		retMode = iotDevPropsSet.deviceMode;
	}
	
	if((*evt) == IOT_FAN_EVENT)
	{
		retMode = eWorkNormal;
		fanLevel = iotDevPropsSet.fanLevel;
		beep_on();			
	}
	
	if(modeStep == 0)
	{
		fanLevelRgbDisp(fanLevel);
		if((*evt) == BTN_MODE_EVENT)
		{
			beep_on();
			if(fanLevel < 3)
			{
				fanLevel += 1;
				fan_level_control(fanLevel);
			}
			else
			{
				retMode = eWorkAuto;
			}
			normalModeTick = 0;
		} 

		if(device_timeout_count(&normalModeTick, 5000) ==  TIME_OUT_WORK)
		{
			modeStep = 1;
		}
	}
	else if(modeStep == 1)
	{
		airQualityRgbDisp();
		if((*evt) == BTN_MODE_EVENT)
		{
			beep_on();
			if(fanLevel < 3)
			{
				fanLevel += 1;
				fan_level_control(fanLevel);
			}
			else
			{
				retMode = eWorkAuto;
			}
			modeStep = 0;
			normalModeTick = 0;
		} 
	}
	
	if(((*evt) == BTN_AMD_EVENT) || ((*evt) == IOT_AMD_EVENT))
	{
		disinfect_status_invert();

		tm1668_flag_set(ePanelFlagDisinfect, disinfect_status_get());
		beep_on();
		printf("disinfect status:%s\r\n", disinfect_status_get()?"on":"off");
	}

	if(((*evt) == BTN_ION_EVENT) || ((*evt) == IOT_ION_EVENT))
	{
		ion_status_invert();
		tm1668_flag_set(ePanelFlagIon, ion_status_get());
		beep_on();
		printf("ion status:%s\r\n", ion_status_get()?"on":"off");
	}
	
	if((*evt) == BTN_MATCH_NETWORK_EVENT)
	{
		//beep_on();
		iot_request_flag = 0x01;
	}
	
	
	return retMode;
}

void DeviceProcess(uint32_t* event)
{
	
	if((*event))
	{
		printf("event point value:%lu\r\n", (*event));
			iot_state_updata_flag = 1;
	}

	if(FirstBootFlag == FALSE)
	{
		if(device_timeout_count(&standbyTestTick, 30000) ==  TIME_OUT_WORK )
		{
			FirstBootFlag = TRUE;
		}
	}
	
	deviceStatus[deviceSMPara.deviceStatus].function(&deviceSMPara, event);
	
	if(deviceSMPara.deviceStatus != retStatus)
	{
		printf("Device status change form [%s] to [%s].\r\n", deviceStatus[deviceSMPara.deviceStatus].string, deviceStatus[retStatus].string);
		deviceSMPara.lastDeviceStatus = deviceSMPara.deviceStatus;
		deviceSMPara.deviceStatus = retStatus;
		//deviceSMPara.statuSwitchFlag = TRUE;
		statuSwitchFlag_test = TRUE;
	}
	
	if(deviceSMPara.deviceStatus == eDeviceWork)
		iotDevProps.deviceSwitch = 1;
	else 
		iotDevProps.deviceSwitch = 0;

	iotDevProps.anionSwitch = disinfect_status_get();
	iotDevProps.plasmaSwitch = ion_status_get();
	iotDevProps.pm2d5Value = pm2d5DispValue;
	iotDevProps.airQualityValue = airQualityLevel;
	iotDevProps.deviceMode = workSMPara.workStatus;
	iotDevProps.fanLevel = fanLevel;
	

	(*event) = 0;
}

